package com.ruoyi.web.controller.base;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.constant.DictConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.BaseMaterial;
import com.ruoyi.system.mapper.BaseMaterialMapper;
import com.ruoyi.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IBaseMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料信息Controller
 *
 * @author ruoyi
 * @date 2023-02-01
 */
@Api(tags = "物料信息api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/material" )
public class BaseMaterialController extends BaseController {

    private final IBaseMaterialService iBaseMaterialService;

    private ISysDictTypeService dictTypeService;

    /**
     * 查询物料信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseMaterial baseMaterial)
    {
        startPage();
        LambdaQueryWrapper<BaseMaterial> lqw = new LambdaQueryWrapper<BaseMaterial>();
        if (StringUtils.isNotBlank(baseMaterial.getMaterialCode())){
            lqw.eq(BaseMaterial::getMaterialCode ,baseMaterial.getMaterialCode());
        }
        if (StringUtils.isNotBlank(baseMaterial.getMaterialName())){
            lqw.like(BaseMaterial::getMaterialName ,baseMaterial.getMaterialName());
        }
        if (StringUtils.isNotBlank(baseMaterial.getMaterialType())){
            lqw.eq(BaseMaterial::getMaterialType ,baseMaterial.getMaterialType());
        }
        if (StringUtils.isNotBlank(baseMaterial.getMaterialDescribe())){
            lqw.eq(BaseMaterial::getMaterialDescribe ,baseMaterial.getMaterialDescribe());
        }
        if (StringUtils.isNotBlank(baseMaterial.getMaterialGroup())){
            lqw.eq(BaseMaterial::getMaterialGroup ,baseMaterial.getMaterialGroup());
        }
        if (StringUtils.isNotBlank(baseMaterial.getMaterialUnit())){
            lqw.eq(BaseMaterial::getMaterialUnit ,baseMaterial.getMaterialUnit());
        }
        if (StringUtils.isNotBlank(baseMaterial.getMaterialFace())){
            lqw.eq(BaseMaterial::getMaterialFace ,baseMaterial.getMaterialFace());
        }
        if (StringUtils.isNotBlank(baseMaterial.getIsTracing())){
            lqw.eq(BaseMaterial::getIsTracing ,baseMaterial.getIsTracing());
        }
        if (baseMaterial.getSupplierId() != null){
            lqw.eq(BaseMaterial::getSupplierId ,baseMaterial.getSupplierId());
        }
        if (baseMaterial.getValidTime() != null){
            lqw.eq(BaseMaterial::getValidTime ,baseMaterial.getValidTime());
        }
        List<BaseMaterial> list = iBaseMaterialService.list(lqw);
            list.forEach(item->{
                String dictLabel = DictUtils.getDictLabel(DictConstants.assembly_type, item.getAssemblyType()==null?"":item.getAssemblyType());
                if(StrUtil.isNotEmpty(dictLabel)){
                    item.setAssemblyType(dictLabel) ;
                }
            });
        return getDataTable(list);
    }

    /**
     * 查询物料信息列表所有数据
     */
    @GetMapping("/getList")
    public List<BaseMaterial> getList(BaseMaterial baseMaterial)
    {
        LambdaQueryWrapper<BaseMaterial> lqw = new LambdaQueryWrapper<BaseMaterial>();
        if (StringUtils.isNotBlank(baseMaterial.getMaterialCode())){
            lqw.eq(BaseMaterial::getMaterialCode ,baseMaterial.getMaterialCode());
        }
        if (StringUtils.isNotBlank(baseMaterial.getMaterialName())){
            lqw.like(BaseMaterial::getMaterialName ,baseMaterial.getMaterialName());
        }
        if (StringUtils.isNotBlank(baseMaterial.getMaterialType())){
            lqw.eq(BaseMaterial::getMaterialType ,baseMaterial.getMaterialType());
        }
        if (StringUtils.isNotBlank(baseMaterial.getMaterialDescribe())){
            lqw.eq(BaseMaterial::getMaterialDescribe ,baseMaterial.getMaterialDescribe());
        }
        if (StringUtils.isNotBlank(baseMaterial.getMaterialGroup())){
            lqw.eq(BaseMaterial::getMaterialGroup ,baseMaterial.getMaterialGroup());
        }
        if (StringUtils.isNotBlank(baseMaterial.getMaterialUnit())){
            lqw.eq(BaseMaterial::getMaterialUnit ,baseMaterial.getMaterialUnit());
        }
        if (StringUtils.isNotBlank(baseMaterial.getMaterialFace())){
            lqw.eq(BaseMaterial::getMaterialFace ,baseMaterial.getMaterialFace());
        }
        if (StringUtils.isNotBlank(baseMaterial.getIsTracing())){
            lqw.eq(BaseMaterial::getIsTracing ,baseMaterial.getIsTracing());
        }
        if (baseMaterial.getSupplierId() != null){
            lqw.eq(BaseMaterial::getSupplierId ,baseMaterial.getSupplierId());
        }
        if (baseMaterial.getValidTime() != null){
            lqw.eq(BaseMaterial::getValidTime ,baseMaterial.getValidTime());
        }
        List<BaseMaterial> list = iBaseMaterialService.list(lqw);
        return list;
    }

    /**
     * 导出物料信息列表
     */
    @Log(title = "物料信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BaseMaterial baseMaterial) {
        LambdaQueryWrapper<BaseMaterial> lqw = new LambdaQueryWrapper<BaseMaterial>(baseMaterial);
        List<BaseMaterial> list = iBaseMaterialService.list(lqw);
        ExcelUtil<BaseMaterial> util = new ExcelUtil<BaseMaterial>(BaseMaterial. class);
        return util.exportExcel(list, "material" );
    }

    /**
     * 获取物料信息详细信息
     */
    @ApiOperation(value = "获取物料信息详细信息")
    @GetMapping(value = "/{materialId}" )
    public AjaxResult getInfo(@PathVariable("materialId" ) Long materialId) {
        return AjaxResult.success(iBaseMaterialService.getById(materialId));
    }

    @Autowired
    private BaseMaterialMapper baseMaterialMapper;
    /**
     * 新增物料信息
     */
    @Log(title = "物料信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseMaterial baseMaterial) {
        baseMaterial.setCreateBy(SecurityUtils.getUsername());
        baseMaterial.setCreateTime(new Date());
        //新增物料编码与物料名称不能重复
        if(StrUtil.isNotEmpty(baseMaterial.getMaterialCode())){
            LambdaQueryWrapper<BaseMaterial> lqw = new LambdaQueryWrapper<BaseMaterial>();
            lqw.eq(BaseMaterial::getMaterialCode,baseMaterial.getMaterialCode());
            List<BaseMaterial> list = baseMaterialMapper.selectList(lqw);
            if(IterUtil.isNotEmpty(list)){
                throw new CustomException("物料编码重复");
            }
        }
        if(StrUtil.isNotEmpty(baseMaterial.getMaterialName())){
            LambdaQueryWrapper<BaseMaterial> lqw = new LambdaQueryWrapper<BaseMaterial>();
            lqw.eq(BaseMaterial::getMaterialName,baseMaterial.getMaterialName());
            List<BaseMaterial> list = baseMaterialMapper.selectList(lqw);
            if(IterUtil.isNotEmpty(list)){
                throw new CustomException("物料名称重复");
            }
        }
        return toAjax(iBaseMaterialService.save(baseMaterial) ? 1 : 0);
    }

    /**
     * 修改物料信息
     */
    @Log(title = "物料信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseMaterial baseMaterial) {
        return toAjax(iBaseMaterialService.updateById(baseMaterial) ? 1 : 0);
    }

    /**
     * 删除物料信息
     */
    @Log(title = "物料信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialIds}" )
    public AjaxResult remove(@PathVariable Long[] materialIds) {
        return toAjax(iBaseMaterialService.removeByIds(Arrays.asList(materialIds)) ? 1 : 0);
    }
}
